module(..., package.seeall)

--[[
	Saves specified value to specified file
--]]
function saveValue(strFilename, strValue)
    local theFile = strFilename;
    local theValue = strValue;
    local path = system.pathForFile(theFile, system.DocumentsDirectory);
    -- io.open opens a file at path. returns nil if no file found
    local file = io.open(path, "w+");
    if (file) then
       -- write game score to the text file
       file:write(theValue);
       io.close(file);
    end
end

--[[
	Loads specified file, or create new file if it doesn't exist
--]]
function loadValue(strFilename)
    local theFile = strFilename;
    local path = system.pathForFile(theFile, system.DocumentsDirectory);
    -- io.open opens a file at path. returns nil if no file found
    local file = io.open(path, "r");
    if (file) then
       -- read all contents of file into a string
       local contents = file:read("*a");
       io.close(file);
       return contents;
    else
       -- create file b/c it doesn't exist yet
       file = io.open( path, "w");
       file:write("0");
       io.close(file);
       return "0";
    end
end

--[[
	Reset data base used when the user start a new game
--]]
function resetData()
	print("reset data")
	saveValue(LEVEL_SAVE_FILE,"0")
	saveValue(HIGH_SCORE_SAVE_FILE,"0")
	for contLevel=1,LAST_LEVEL do
		saveValue(HIGH_SCORE_SAVE_FILE..contLevel,"0")
	end
end